package ar.frba.utn.diseñosistemas.TP;

import java.util.Calendar;
import java.util.Date;

public class UnifiedDate 
{	
	private Calendar innerDate;
	
	public UnifiedDate(Date baseDate)
	{
		this.innerDate = Calendar.getInstance();
		this.innerDate.setTime(baseDate);
	}
	
	public UnifiedDate(int aDay, int aMonth, int aYear) {
		this.innerDate = Calendar.getInstance();
		this.innerDate.set(aYear, aMonth, aDay);
	}

	public Calendar getAsCalendar()
	{
		return this.innerDate;
	}
	public Date getAsDate()
	{
		return this.innerDate.getTime();
	}
	
	public int getDay() {
		return this.innerDate.get(Calendar.DAY_OF_MONTH);
	}
	public void setDay(int newDay) throws Exception {
		this.innerDate.set(Calendar.DAY_OF_MONTH, newDay);
	}
	
	public int getMonth() {
		return this.innerDate.get(Calendar.MONTH);
	}
	public void setMonth(int newMonth) throws Exception {
		this.innerDate.set(Calendar.MONTH, newMonth);
	}
	
	public int getYear() {
		return this.innerDate.get(Calendar.YEAR);
	}
	public void setYear(int newYear) throws Exception {
		this.innerDate.set(Calendar.YEAR, newYear);
	}

	@Override
	public boolean equals(Object obj)
	{
		if (!(obj instanceof UnifiedDate))
			return false;
		
		UnifiedDate otherDate = (UnifiedDate) obj;
		
		return this.getAsDate().equals(otherDate.getAsDate());
	}

	public boolean isBeforeThan(UnifiedDate otherDate)
	{
		return this.innerDate.before(otherDate.getAsCalendar());
	}

	public long daysBetween(UnifiedDate otherDate)
	{
		long thisTotalMilliSeconds = this.innerDate.getTimeInMillis();
		long otherTotalMilliSeconds = otherDate.getAsCalendar().getTimeInMillis();
		
		long diference = Math.abs(thisTotalMilliSeconds - otherTotalMilliSeconds); 
		
		return this.MillisecondsToDays(diference);
	}
	
	private long MillisecondsToDays(long milliSeconds)
	{
		return milliSeconds / 1000 / 60 / 60 / 24;
		//				   > Seg  > Min > Horas > Dias
	}
}
